﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HROReporting.Infrastructure.Authentication;
using HROReporting.Services.Interfaces;
using HROReporting.Infrastructure.CookieStorage;
using System.Web.Mvc;
using HROReporting.Controllers.ViewModels.Vergader;
using HROReporting.Services.Messaging.SchoolLocatieService;
using HROReporting.Controllers.ViewModels.Reservering;
using HROReporting.Model.Lesuren;
using HROReporting.Services.Messaging.StudentService;

namespace HROReporting.Controllers.Controllers
{
    public class VergaderController : BaseController
    {
        private readonly IFormsAuthentication _formsAuthentication;
        private readonly ISchoolLocatieService _schoolLocatieService;
        private readonly ILesuurService _lesuurService;
        private readonly IStudentService _studentService;

        public VergaderController(ICookieStorageService cookieStorageService,
                                IFormsAuthentication formsAuthentication,
                                ISchoolLocatieService schoolLocatieService,
                                ILesuurService lesuurService,
                                IStudentService studentService)
            : base (cookieStorageService)
        {
            _formsAuthentication = formsAuthentication;
            _schoolLocatieService = schoolLocatieService;
            _lesuurService = lesuurService;
            _studentService = studentService;
        }
        [Authorize]
        public ActionResult SchoolLocatieIndex()
        {
            SchoolLocatieIndexView vergaderIndexView = new SchoolLocatieIndexView();

            GetAllLocatiesResponse response = _schoolLocatieService.GetAllSchoolLocaties();

            vergaderIndexView.SchoolLocaties = response.SchoolLocatieViews;

            return View(vergaderIndexView);
        }

        [Authorize]
        public ActionResult VergaderIndex(string id)
        {
            VergaderIndexView vergaderIndexView = new VergaderIndexView();

            GetSchoolLocatieByRequest request = new GetSchoolLocatieByRequest();
            request.SchoolLocatie = id;

            GetSchoolLocatieByResponse response = _schoolLocatieService.GetSchoolLocatieByLocatie(request);

            vergaderIndexView.SchoolLocatie = response.SchoolLocatie;

            return View(vergaderIndexView);
        }

        [Authorize]
        public ActionResult VergaderDetails(string locatie, string code)
        {
            VergaderDetailView vergaderDetailsView = new VergaderDetailView();

            GetVergaderRuimteByRequest request = new GetVergaderRuimteByRequest();
            request.SchoolLocatie = locatie;
            request.VergaderRuimteCode = code;

            GetVergaderRuimteByResponse response = _schoolLocatieService.GetVergaderRuimteBy(request);

            vergaderDetailsView.VergaderRuimte = response.VergaderRuimte;
            vergaderDetailsView.VergaderRuimteCode = code;

            return View(vergaderDetailsView);
        }

        [Authorize]
        public ActionResult VergaderReserveer(string locatie, string code)
        {
            VergaderDetailView vergaderReserveerView = new VergaderDetailView();

            GetVergaderRuimteByRequest request = new GetVergaderRuimteByRequest();
            request.SchoolLocatie = locatie;
            request.VergaderRuimteCode = code;

            GetVergaderRuimteByResponse response = _schoolLocatieService.GetVergaderRuimteBy(request);

            vergaderReserveerView.VergaderRuimte = response.VergaderRuimte;
            vergaderReserveerView.VergaderRuimteCode = code;


            return View(vergaderReserveerView);
        }

        //Reservering plaatsen
        public ActionResult Create(string locatie, string code)
        {
            VergaderCreateView vergaderCreateView = new VergaderCreateView();

            GetVergaderRuimteByRequest request = new GetVergaderRuimteByRequest();
            request.SchoolLocatie = locatie;
            request.VergaderRuimteCode = code;

            GetVergaderRuimteByResponse response = _schoolLocatieService.GetVergaderRuimteBy(request);


            //vergaderCreateView.VergaderRuimte = response.VergaderRuimte;
            //vergaderCreateView.VergaderRuimteId = response.VergaderRuimte;

            IEnumerable<Lesuur> lesuren = _lesuurService.FindAll();

            vergaderCreateView.LesStartUren = lesuren;
            vergaderCreateView.LesEindUren = lesuren;

            return View(vergaderCreateView);
        }
        
        //Reservering plaatsen (POST)
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(VergaderCreateView vergaderCreateView, string locatie, string code)
        {
            GetStudentRequest studentRequest = new GetStudentRequest();
            studentRequest.StudentIdentityToken = _formsAuthentication.GetAuthenticationToken();
            //GetStudentResponse studentResponse = _studentService.GetStudentByAuthenticationToken(studentRequest);
            /*
            GetUserRequest userRequest = new GetUserRequest();
            userRequest.UserIdentityToken = _formsAuthentication.GetAuthenticationToken();
            GetUserResponse userResponse = _studentService.GetUserResponseById(userRequest);
            */            

            GetVergaderRuimteByRequest vergaderRuimteRequest = new GetVergaderRuimteByRequest();
            vergaderRuimteRequest.SchoolLocatie = locatie;
            vergaderRuimteRequest.VergaderRuimteCode = code;
            //vergaderRuimteRequest.VergaderRuimteCode = vergaderCreateView.SelectedVergaderRuimte;
            
            GetVergaderRuimteByResponse vergaderRuimteReponse = _schoolLocatieService.GetVergaderRuimteBy(vergaderRuimteRequest);

            Lesuur lesuurStart = _lesuurService.FindBy(Convert.ToInt16(vergaderCreateView.SelectedLesuurStart));
            Lesuur lesuurEind = _lesuurService.FindBy(Convert.ToInt16(vergaderCreateView.SelectedLesuurEind));

            CreateReserveringRequest createReserveringRequest = new CreateReserveringRequest();
            //createReserveringRequest.User = userResponse.User;
            //createReserveringRequest.VergaderRuimte = vergaderRuimteReponse.VergaderRuimte;
            createReserveringRequest.LesuurStart = lesuurStart;
            createReserveringRequest.LesuurEind = lesuurEind;
            createReserveringRequest.Datum = DateTime.Now;

            //_studentService.CreateReservering(createReserveringRequest);

            return View();
        }
         
    }
}
